<template>
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">基于自动化运维信息系统深度分析</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="账号"
        >
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter.native="handleLogin"
        >
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <!--       <el-form-item prop="code" v-if="captchaEnabled">-->
      <!--        <el-input-->
      <!--          v-model="loginForm.code"-->
      <!--          auto-complete="off"-->
      <!--          placeholder="验证码"-->
      <!--          style="width: 63%"-->
      <!--          @keyup.enter.native="handleLogin"-->
      <!--        >-->
      <!--          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
      <!--        </el-input>-->
      <!--        <div class="login-code">-->
      <!--          <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
      <!--        </div>-->
      <!--      </el-form-item>-->
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <!-- <div style="float: right;">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div> -->
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>

<script>
  window._interceptConfig = {
    collection: true, //整体开关
    click: true, //页面点击事件信息采集开关
    page: true, //页面跳转加载事件信息采集开关
    error: true, //异常信息采集开关
    http: true, //通讯信息采集开关
    name: "jyzdhywxxxtsdfx_n_prd.mart-ui-ds", //系统编号，被采集信息的系统名称。前端简称
    url: " http://apm.sd.sgcc.com.cn:19411/api/v2/spansWeb"//数据上报服务地址，根据实际情况输入
  };
</script>
<!-- src -->
<script src=" http://25.41.37.249:19411/assets/js/intercept.min.js " async></script>
<script>
  import { getCodeImg,getPublicKey,insertPublicKey } from "@/api/login";
  import Cookies from "js-cookie";
  import { encrypt, decrypt } from '@/utils/jsencrypt'
  import cache from '@/plugins/cache'
  export default {
    name: "Login",
    data() {
      return {
        codeUrl: "",
        loginForm: {
          username: "",
          password: "",
          rememberMe: false,
          code: "",
          uuid: "",
          status:"",
        },
        loginRules: {
          username: [
            { required: true, trigger: "blur", message: "请输入您的账号" }
          ],
          password: [
            { required: true, trigger: "blur", message: "请输入您的密码" }
          ],
          code: [{ required: true, trigger: "change", message: "请输入验证码" }]
        },
        loading: false,
        // 验证码开关
        captchaEnabled: true,
        // 注册开关
        register: false,
        redirect: undefined
      };
    },
    watch: {
      $route: {
        handler: function(route) {
          this.redirect = route.query && route.query.redirect;
        },
        immediate: true
      }
    },
    created() {
      // this.getCode();
      //this.getCookie();
      // if (this.$route.query.un && this.$route.query.pwd && this.$route.query.ypassword) {
      // if (this.$route.query.un && this.$route.query.pwd ) {
      //   // ypassword为请求盐值   若盐值不同则拒绝请求
      //   //if(this.$route.query.ypassword === process.env.VUE_APP_JMM){


      // console.log(this.$route.query.un)

      //   var publicKey = process.env.VUE_APP_JM
      //   console.log("打印信息 解密的公钥 {}" ,publicKey)
      //   console.log("打印信息 redirect {}" ,this.redirect)
      //   //cache.session.set("pub",publicKey)
      //   this.loginForm.username = this.$route.query.un
      //   this.loginForm.password = this.$route.query.pwd

      //   this.loginForm.status = "1"
      //   //let keypair = sm2.generateKeyPairHex()
      //   //let qpubkey = keypair.publicKey // 公钥
      //   //let qprikey = keypair.privateKey // 私钥
      //   //console.log("撕月",qprikey)
      //   //console.log("gongyue",qpubkey)
      //   //this.loginForm.password = sm2.doEncrypt(this.$route.query.pwd, publicKey, cipherMode);
      //   console.log("打印信息 this.loginForm.password {}" ,this.$route.query.pwd)
      //   //这里开始登陆




      //   this.$store.dispatch("Login", this.loginForm).then(() => {
      //     console.log("请求对应的接口")
      //     console.log("请求对应的接口")
      //     console.log(this.redirect)
      //     this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
      //   }).catch(() => {
      //     this.loading = false;
      //     // if (this.captchaEnabled) {
      //     //   this.getCode();
      //     // }
      //   });
      //   //}
      // }else {
      //   let result = "";
      //   //直接首位判断  含有 25.41.68.218:18080/?ticket=ST-1506992-XGxpekNiJKoKLb-iscsso.sd.sgcc.com.cn#/login?redirect=%2Findex
      //   //var uurl  =  "25.41.68.218:18080/?ticket=ST-1506992-XGxpekNiJKoKLb-iscsso.sd.sgcc.com.cn#/login?redirect=%2Findex"

      //   var navigation = window.location.href
      //   var ticket = navigation.indexOf("ticket");
      //   var jin = navigation.indexOf("#")



      //   if(ticket > 0){
      //     // 含有ticket 就准备跳转
      //     result = navigation.substring(ticket + 7,navigation.indexOf("#"))

      //   }else {
      //     //没有值，便应该跳转 ！！！
      //   //  let url = 'http://iscsso.sd.sgcc.com.cn/isc_sso/login?service=http://25.41.68.218:18080' // 这里的地址前面就不用加协议了'http:// 或者 https://' 下面一句话就是处理这个的。
      //     //let path = 'http://www.baidu.com'
      //     //let path = window.location.protocol +  "//" + url
      //     //http://iscsso.sd.sgcc.com.cn/isc_sso/login?service=25.41.68.218:18080
      //     //window.location.href = url
      //   //  window.open(url,'_self')
      //   }

      //   if(result !=null){

      //     //进行跳转
      //     const sm2 = require('sm-crypto').sm2

      //     let keypair = sm2.generateKeyPairHex()
      //     let qpubkey = keypair.publicKey // 公钥
      //     let qprikey = keypair.privateKey // 私钥

      //     //console.log("==================" , qpubkey)
      //     //console.log("siyao",qprikey)
      //     //console.log("gongyao",qpubkey)
      //     cache.session.set("qdPri",qprikey)
      //     //传递前端公钥 调用接口
      //     //console.log("开始")
      //     // insertPublicKey(qpubkey).then(response => {
      //     //   console.log(response)
      //     // });

      //     getPublicKey().then(res => {

      //       //console.log("res打印",res)
      //       const cipherMode = 1;// 1 - C1C3C2，0 - C1C2C3，默认为1
      //       //1.取后端的公钥
      //       var publicKey =  res.pubKey;
      //       // 2.服务器上的时间
      //       var timestampa = res.timestamps;
      //       // 4.拿取本地的时间戳 ,记录Δtimestamp
      //       var time = timestampa -  new Date().getTime();
      //       cache.session.set("time",time)
      //       cache.session.set("pub",publicKey)
      //       //传送前端公钥  这时候 私钥是公开的   后端的公钥是公开的    用后端公钥加密前端公钥
      //       insertPublicKey(sm2.doEncrypt(qpubkey, publicKey, cipherMode)).then(res=>{
      //         var uid = res.uid;
      //         //返回前端公钥加密的uid  用前端私钥解密
      //         var res = sm2.doDecrypt(uid,qprikey,1)
      //         cache.session.set("uid",res)
      //         this.loginForm.username = "sd_admin"
      //         this.loginForm.password = sm2.doEncrypt("admin123", publicKey, cipherMode);
      //         //this.loginForm.username = sm2.doEncrypt(this.loginForm.username, publicKey, cipherMode);
      //         this.$store.dispatch("Login", this.loginForm).then(() => {
      //           console.log("产看返回值" , this.redirect)
      //           this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
      //         }).catch(() => {
      //           this.loading = false;
      //         });
      //       })
      //     }).catch(error => {
      //       //console.log("出问题了",error)
      //     })

      //   }
      // }
        let result = "";
        //直接首位判断  含有 25.41.68.218:18080/?ticket=ST-1506992-XGxpekNiJKoKLb-iscsso.sd.sgcc.com.cn#/login?redirect=%2Findex
        //var uurl  =  "25.41.68.218:18080/?ticket=ST-1506992-XGxpekNiJKoKLb-iscsso.sd.sgcc.com.cn#/login?redirect=%2Findex"

        var navigation = window.location.href
        var ticket = navigation.indexOf("ticket");
        var loginx = navigation.indexOf("loginx");
        // var jin = navigation.indexOf("#")
        if(ticket > 0){
          // // 含有ticket 就准备跳转
          // //使用统一权限用户登录
          // //     //进行跳转
          // const sm2 = require('sm-crypto').sm2

          // let keypair = sm2.generateKeyPairHex()
          // let qpubkey = keypair.publicKey // 公钥
          // let qprikey = keypair.privateKey // 私钥

          // //console.log("==================" , qpubkey)
          // //console.log("siyao",qprikey)
          // //console.log("gongyao",qpubkey)
          // cache.session.set("qdPri",qprikey)
          // //传递前端公钥 调用接口
          // //console.log("开始")
          // // insertPublicKey(qpubkey).then(response => {
          // //   console.log(response)
          // // });

          // getPublicKey().then(res => {

          //   //console.log("res打印",res)
          //   const cipherMode = 1;// 1 - C1C3C2，0 - C1C2C3，默认为1
          //   //1.取后端的公钥
          //   var publicKey =  res.pubKey;
          //   // 2.服务器上的时间
          //   var timestampa = res.timestamps;
          //   // 4.拿取本地的时间戳 ,记录Δtimestamp
          //   var time = timestampa -  new Date().getTime();
          //   cache.session.set("time",time)
          //   cache.session.set("pub",publicKey)
          //   //传送前端公钥  这时候 私钥是公开的   后端的公钥是公开的    用后端公钥加密前端公钥
          //   insertPublicKey(sm2.doEncrypt(qpubkey, publicKey, cipherMode)).then(res=>{
          //     var uid = res.uid;
          //     //返回前端公钥加密的uid  用前端私钥解密
          //     var res = sm2.doDecrypt(uid,qprikey,1)
          //     cache.session.set("uid",res)
              this.loginForm.username = "tongyiquanxian";
              this.loginForm.password = "tongyiquanxian";
              //this.loginForm.username = sm2.doEncrypt(this.loginForm.username, publicKey, cipherMode);
              this.$store.dispatch("Login", this.loginForm).then(() => {
                console.log("产看返回值" , this.redirect)
                this.$router.push({ path:"/" }).catch(()=>{});
              }).catch(() => {
                this.loading = false;
              });
          // result = navigation.substring(ticket + 7,navigation.indexOf("#"))
        }else if(loginx > 0){
          //没有值，便应该跳转 ！！！
          this.$router.push({ path: "/loginxs" })
        }else {
          //没有值，便应该跳转 ！！！
          let url = 'http://iscsso.sd.sgcc.com.cn/isc_sso/login?service=http://25.41.68.218:18080' // 这里的地址前面就不用加协议了'http:// 或者 https://' 下面一句话就是处理这个的。
          let path = window.location.protocol +  "//" + url
          // http://iscsso.sd.sgcc.com.cn/isc_sso/login?service=25.41.68.218:18080
          window.location.href = url
          window.open(url,'_self')
        }

    },
    methods: {
      // getCode() {
      //   getCodeImg().then(res => {
      //     this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
      //     if (this.captchaEnabled) {
      //       this.codeUrl = "data:image/gif;base64," + res.img;
      //       this.loginForm.uuid = res.uuid;
      //     }
      //   });
      // },
      getCookie() {
        const username = Cookies.get("username");
        const password = Cookies.get("password");
        const rememberMe = Cookies.get('rememberMe')
        this.loginForm = {
          username: username === undefined ? this.loginForm.username : username,
          password: password === undefined ? this.loginForm.password : decrypt(password),
          rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
        };
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid ) {
            //this.loginForm.code = result
            this.loading = true;
            if (this.loginForm.rememberMe) {
              Cookies.set("username", this.loginForm.username, { secure: true });
              Cookies.set("password", encrypt(this.loginForm.password), { secure: true });
              Cookies.set('rememberMe', this.loginForm.rememberMe, { secure: true });
              Cookies.set(true)
            } else {
              Cookies.remove("username");
              Cookies.remove("password");
              Cookies.remove('rememberMe');
            }
                this.$store.dispatch("Login", this.loginForm).then(() => {
                  console.log("产看返回值" , this.redirect)
                  this.$router.push("/" ).catch(()=>{});
                }).catch(() => {
                  this.loading = false;
                  // if (this.captchaEnabled) {
                  //   this.getCode();
                  // }
                });

          }
        });
      }

      // handleLogin() {
      //   this.$refs.loginForm.validate(valid => {


      //     if (valid ) {
      //       //this.loginForm.code = result
      //       this.loading = true;
      //       if (this.loginForm.rememberMe) {
      //         Cookies.set("username", this.loginForm.username, { secure: true });
      //         Cookies.set("password", encrypt(this.loginForm.password), { secure: true });
      //         Cookies.set('rememberMe', this.loginForm.rememberMe, { secure: true });
      //         Cookies.set(true)
      //       } else {
      //         Cookies.remove("username");
      //         Cookies.remove("password");
      //         Cookies.remove('rememberMe');
      //       }

      //       const sm2 = require('sm-crypto').sm2

      //       let keypair = sm2.generateKeyPairHex()
      //       let qpubkey = keypair.publicKey // 公钥
      //       let qprikey = keypair.privateKey // 私钥

      //       //console.log("==================" , qpubkey)
      //       //console.log("siyao",qprikey)
      //       //console.log("gongyao",qpubkey)
      //       cache.session.set("qdPri",qprikey)
      //       //传递前端公钥 调用接口
      //       //console.log("开始")
      //       // insertPublicKey(qpubkey).then(response => {
      //       //   console.log(response)
      //       // });


      //       getPublicKey().then(res => {

      //         //console.log("res打印",res)
      //         const cipherMode = 1;// 1 - C1C3C2，0 - C1C2C3，默认为1
      //         //1.取后端的公钥
      //         var publicKey =  res.pubKey;



      //         // 2.服务器上的时间
      //         var timestampa = res.timestamps;
      //         // 4.拿取本地的时间戳 ,记录Δtimestamp
      //         var time = timestampa -  new Date().getTime();
      //         //console.log("打印当前的时间验证==========" , new Date().getTime())
      //         // console.log("打印当前的时间验证==========" , timestampa)
      //         /// console.log("打印当前的时间验证==========" , time)
      //         cache.session.set("time",time)
      //         cache.session.set("pub",publicKey)
      //         //传送前端公钥  这时候 私钥是公开的   后端的公钥是公开的    用后端公钥加密前端公钥
      //         insertPublicKey(sm2.doEncrypt(qpubkey, publicKey, cipherMode)).then(res=>{
      //           //  console.log("asdfasdfasdf" ,res)
      //           var uid = res.uid;
      //           //返回前端公钥加密的uid  用前端私钥解密
      //           var res = sm2.doDecrypt(uid,qprikey,1)
      //           cache.session.set("uid",res)
      //           this.loginForm.password = sm2.doEncrypt(this.loginForm.password, publicKey, cipherMode);
      //           //console.log("结束")
      //           //this.loginForm.username = sm2.doEncrypt(this.loginForm.username, publicKey, cipherMode);
      //           this.$store.dispatch("Login", this.loginForm).then(() => {
      //             console.log("产看返回值" , this.redirect)
      //             this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
      //           }).catch(() => {
      //             this.loading = false;
      //             // if (this.captchaEnabled) {
      //             //   this.getCode();
      //             // }
      //           });
      //         })
      //       }).catch(error => {
      //         //console.log("出问题了",error)
      //       })

      //     }
      //   });
      // }
    }
  };
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/login-background.jpg");
    background-size: cover;
  }
  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 400px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    height: 38px;
    float: right;
    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
  .login-code-img {
    height: 38px;
  }
</style>
